﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MeTalker.Aide.data;

namespace MeTalker.Aide.Forms
{
    internal partial class frmBindMenu : MeForm
    {
        public frmBindMenu()
        {
            InitializeComponent();
            this.dataGridView.AutoGenerateColumns = false;
            BindData();
        }
        void BindData()
        {
            var d = from temp in DataFactory.SystemConfig.UserConfigs where temp.GuideMenu != 0 select temp;
            List<UserConfig> configs = d.ToList();
            this.dataGridView.DataSource = configs;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            DialogResult result = new frmAddNavigation().ShowDialog();
            if (result == DialogResult.Yes)
                BindData();
        }

        private void dataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex != -1) //单元格当然要排除列头
            {
                // DataGridViewCell cell = this.dataGridView[0, e.RowIndex];
                UserConfig config = this.dataGridView.CurrentRow.DataBoundItem as UserConfig;
                //也要判断当前点击单元格的名称是否是Link列的名称
                if (this.dataGridView.Columns[e.ColumnIndex].Name == "edit" && config != null)
                {
                    List<UserConfig> lists = dataGridView.DataSource as List<UserConfig>;
                    if (lists != null)
                    {
                        DialogResult result = new frmAddNavigation(config).ShowDialog();
                        if (result == DialogResult.Yes)
                        {
                            BindData();
                        }
                    }
                }
                else if (this.dataGridView.Columns[e.ColumnIndex].Name == "delete" && config != null)
                {
                    config.GuideMenu = 0;
                    BindData();
                }
            }
        }

        private void btnHelp_Click(object sender, EventArgs e)
        {
            UtilHelper.RunBrowser(UtilHelper.HELP_BASE_URL + "62.html");
        }
    }
}
